package com.tanhua.api.mongo;

import cn.hutool.core.collection.CollectionUtil;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.mongo.Visitors;
import com.tanhua.domain.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

//    是否是粉丝
    public boolean isFanForUser(Long fanId,Long userId) {
        Query query = new Query(Criteria.where("userId").is(fanId)
                .and("likeUserId").is(userId));
        long count = mongoTemplate.count(query, UserLike.class);
        return  count>0? true : false;
    }

    //1 互相喜欢的数量(tanhua_friends)
    public Long findeachLoveCount(Long userId) {
        Query query = new Query(Criteria.where("UserId").is(userId));
        long count = mongoTemplate.count(query, Friend.class);
        return count;
    }

    //2 喜欢对方的数量(user_like)
    public Long findloveCount(Long userId) {
        Query query = new Query(Criteria.where("UserId").is(userId));
        long count = mongoTemplate.count(query, UserLike.class);
        return count;
    }

    //3 对方喜欢你的数量(user_like)
    public Long findfanCount(Long userId) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        long count = mongoTemplate.count(query, UserLike.class);
        return count;
    }

    //查询互相喜欢 tanhua_friends
    public PageBeanVo findeachLoveList(Integer page, Integer pagesize, Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.with(new Sort(Sort.Direction.DESC,"created"));
        query.skip((page-1)*pagesize).limit(pagesize);
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        long count = mongoTemplate.count(query, Friend.class);
        return new PageBeanVo(page,pagesize,count,friendList);
    }

    //查询当前登录者喜欢的用户 user_like
    public PageBeanVo findloveList(Integer page, Integer pagesize, Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.with(new Sort(Sort.Direction.DESC,"created"));
        query.skip((page-1)*pagesize).limit(pagesize);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);
        return new PageBeanVo(page,pagesize,count,userLikeList);
    }

    //查询当前喜欢我的用户 user_like
    public PageBeanVo findfanList(Integer page, Integer pagesize, Long userId) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        query.with(new Sort(Sort.Direction.DESC,"created"));
        query.skip((page-1)*pagesize).limit(pagesize);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);
        return new PageBeanVo(page,pagesize,count,userLikeList);
    }

    //查询历史访问记录用户 visitors
    public PageBeanVo findHistory(Integer page, Integer pagesize, Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.with(new Sort(Sort.Direction.DESC,"created"));
        query.skip((page-1)*pagesize).limit(pagesize);
        List<Visitors> visitorsList = mongoTemplate.find(query, Visitors.class);
        long count  = mongoTemplate.count(query, Visitors.class);
        return new PageBeanVo(page,pagesize,count,visitorsList);
    }

    //删除粉丝
    public void delete(Long userId, Long fansId) {

        Query query = new Query(Criteria.where("UserId").is(fansId)
        .and("likeUserId").is(userId));
        mongoTemplate.remove(query,UserLike.class);
    }

    //添加回粉丝
            //userId:登录用户的
            //uid:点击取消用户的
    public void save(Long userId, Long uid) {
        UserLike userLike=new UserLike();
        userLike.setId(new ObjectId());
        userLike.setCreated(System.currentTimeMillis());
        userLike.setLikeUserId(userId);
        userLike.setUserId(uid);
        mongoTemplate.insert(userLike);

    }
}
